

var Ajax = {

  _RET_NULL               : " ",
  _CONN_ERROR             : "****",
  _DEFAULT_SEPARATOR_02   : "~^~",

  /**
   * get the assembled composition
   * 
   * @param string  modelIdEnt
   * @return string it can be construct to array using delimiter
   *                using _DEFAULT_SEPARATOR_02
   */
  getModelAssembled : function (modelIdEnt){
  
    var url     = "/ecs/tocmenu_613/controllers/getModelAssembled.php" +"?sid="+Math.random();
    var params  = "modelId=" + modelIdEnt;
    
    var responseText = GAjax.SJax(url,
                                  params);
    //alert(responseText);
    switch(responseText){
            
      case this._CONN_ERROR:
        
        alert("Db connection failed");
        return false;
        
      case this._RET_NULL:
        
        alert("Error: Model Id has no composition");
        return false;
        
      default:
      
        return responseText;
    }
  },
  
  /**
   * is model assembly
   *
   * @param string  modelIdEnt
   * @return boolean true on isAssembly and false on is not
   */
  isModelIdAssembly : function (modelIdEnt){
  
    var url     = "/ecs/tocmenu_613/controllers/isModelIdAssembly.php" +"?sid="+Math.random();
    var params  = "modelId=" + modelIdEnt;
    
    var responseText = GAjax.SJax(url,
                                  params);
    //alert(responseText);
    switch(responseText){
            
      case this._CONN_ERROR:
        
        alert("Db connection failed");
        return false;
        
      case this._RET_NULL:
        
        /**
         * Model Id has no composition
         * return false
         */
        //alert("Error: Model Id has no composition");
        return false;
        
      default:
      
        return true;
    }
  },

  snEntry : function (modelEnt,
                      modelIdEnt,
                      srcQty,
                      tmpJid,
                      srcWhs,
                      srcHostIp){
  
    modelEnt    = document.getElementsByName(modelEnt);
    modelIdEnt  = document.getElementsByName(modelIdEnt);
    srcQty      = document.getElementsByName(srcQty);
    srcWhs      = document.getElementsByName(srcWhs);
    
    modelEnt    = GMisc.firstElement(modelEnt);
    modelIdEnt  = GMisc.firstElement(modelIdEnt);
    srcQty      = GMisc.firstElement(srcQty);
    srcWhs      = GMisc.firstElement(srcWhs);
    
    /**
    * check if assembled
    */
    var ret = this.isModelIdAssembly(modelIdEnt.value);
    
    if(ret == true){
    
      ret = this.getModelAssembled(modelIdEnt.value);
      
      var nModelId = ret.split(this._DEFAULT_SEPARATOR_02);
      
      for(iMdl = 0; iMdl < nModelId; iMdl++){
      
        Misc.snComposEntry(modelIdEnt.value,
                           modelEnt.value,
                           nModelId[iMdl],
                           srcQty.value,
                           tmpJid,
                           srcWhs.value,
                           srcHostIp);
      }
    }else{
    
      Misc.snEntry2(modelEnt.value,
                    modelIdEnt.value,
                    srcQty.value,
                    tmpJid,
                    srcWhs.value,
                    srcHostIp);
    }
  },

  isQtySufficient : function (fromEnt,
						                  modelIdEnt,
						                  thisObj){
						                  
		fromEnt     = document.getElementsByName(fromEnt);
		modelIdEnt  = document.getElementsByName(modelIdEnt);
    
    fromEnt     = GMisc.firstElement(fromEnt);
    modelIdEnt  = GMisc.firstElement(modelIdEnt);
    
    var url     = "/ecs/tocmenu_611/controllers/getIsQtySufficient.php" +"?sid="+Math.random();
    var params  = "whs="      + fromEnt.value +
                  "&modelId=" + modelIdEnt.value +
                  "&qty="     + thisObj.value;
    
    var responseText = GAjax.SJax(url,
                                  params);
    //alert(responseText);
    switch(responseText){
            
      case this._CONN_ERROR:
        
        alert("Db connection failed");
        break;
        
      case this._RET_NULL:
        
        alert("Error Insufficient Balance");
        thisObj.value = "0";
        break;
        
      default:
    }
	},	

  /**
  * modelId 
  *
  * @param  HtmlCollection  modelName
  * @param  HtmlElement     thisObj
  */
  modelId : function (modelName, thisObj){
  
    modelName = document.getElementsByName(modelName);
    
    modelName = GMisc.firstElement(modelName);
    
    var url     = "/ecs/tocmenu_611/controllers/getModelId.php" +"?sid="+Math.random();
    var params  = "modelName="    + modelName.value ;
    
    var responseText = GAjax.SJax(url,
                                  params);
    //alert(responseText);
    switch(responseText){
    
      case this._CONN_ERROR:
      
        alert("Db Connection failed");
        break;
      
      default:
      
        thisObj.value = responseText;
    }
  },

  model : function(whs, thisObj){
  
    whs = document.getElementsByName(whs);
    whs = GMisc.firstElement(whs);

    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_611/controllers/getModel.php" +"?sid="+Math.random();
    var params  = "whs="    + whs.value +
                  "&model=" + thisObj.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {
      
    }
    
    xmlHttp.send(params);
    alert(xmlHttp.responseText);
    switch(xmlHttp.responseText){
      
      case this._CONN_ERROR:
      
        alert("Db Connection failed");
        break;
        
      case this._RET_NULL:
      
        alert("System has given up");
        break;
      
      default:
        thisObj.innerHTML = xmlHttp.responseText;
    }
  },

  /**
  * warehouse list
  *
  * @param  HtmlElement   thisObj
  * @return HtmlElement 
  */
  whs : function(thisObj){
  
    var xmlHttp = new XMLHttpRequest();
    
    var url     = "/ecs/tocmenu_611/controllers/getWhs.php" +"?sid="+Math.random();
    var params  = "whs=" + thisObj.value;
    
    xmlHttp.open("POST", url, false);
    
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    
    xmlHttp.onreadystatechange = function() {
      
    }
    
    xmlHttp.send(params);
    //alert(xmlHttp.responseText);
    switch(xmlHttp.responseText){
      
      case this._CONN_ERROR:
      
        alert("Db Connection failed");
        break;
        
      case this._RET_NULL:
      
        alert("System has given up");
        break;
      
      default:
        thisObj.innerHTML = xmlHttp.responseText;
    }
  },

  /**
  * check if model is SN-ed
  *
  * @param  string  modelId
  * @return boolean
  */
  isModelIdSN : function (modelId){
  
    var url     = "/ecs/tocmenu_611/controllers/getIsModelIdSN.php" +"?sid="+Math.random();
    var params  = "modelId="  + modelId;
    
    var responseText = GAjax.SJax(url,
                                  params);
    //alert(responseText);
    switch(responseText){
    
      case this._CONN_ERROR:
      
        alert("Db Connection failed");
        break;
      
      default:
      
        return responseText;
    }
  },

  sayHello : function(){
  
    alert("Ajax Linked");
  }
} 